package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class Select extends JInternalFrame {

	Connection con;
	DatabaseTest main;
	DefaultTableModel model;
	private JPanel panel;
	private JTextField sqlTF;
	private JButton btnNewButton;
	private JLabel kekka;
	private JScrollPane scrollPane;
	private JTable table;

	public Select(Connection con,DatabaseTest main) {
		super("조회",true,true,true,true);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameClosing(InternalFrameEvent e) {
				sqlTF.setText("");
				if(model != null){
					model.setColumnCount(0);
					model.setRowCount(0);
				}
			}
		});
		this.con=con;
		this.main=main;
		setBounds(100, 100, 450, 300);
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getKekka(), BorderLayout.SOUTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);
		
		JTableHeader jh = table.getTableHeader();
		jh.setReorderingAllowed(false);
		
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.add(getSqlTF());
			panel.add(getBtnNewButton());
		}
		return panel;
	}
	private JTextField getSqlTF() {
		if (sqlTF == null) {
			sqlTF = new JTextField();
			sqlTF.setColumns(20);
		}
		return sqlTF;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("쿼리실행");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					setList();
				}
			});
		}
		return btnNewButton;
	}
	private JLabel getKekka() {
		if (kekka == null) {
			kekka = new JLabel("쿼리를 실행하세요");
		}
		return kekka;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent ev) {
					if(main.update == null){
						main.openUpdate();
					}
					int row = table.getSelectedRow();
					if(model.getColumnCount()==4 && model.getColumnName(1).equals("CODE") 
							&& model.getColumnName(2).equals("EA") && model.getColumnName(3).equals("SERIAL")){
						String c = (String)table.getValueAt(row, 1);
						String e = (String)table.getValueAt(row, 2);
						String s = (String)table.getValueAt(row, 3);
						
						main.update.setData(s,c,e);
						
						if(main.update.isVisible()==false){
							main.contentPane.add(main.update);
						}
						
						main.update.setVisible(true);
						main.update.toFront();
					}

				}
			});
		}
		return table;
	}

	public void setList(){
		try {

			String sql = sqlTF.getText();
			PreparedStatement psmt = con.prepareStatement(sql);
			ResultSet rs = psmt.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();
			int colCount = rsmd.getColumnCount();
			Vector<String> colName = new Vector<String>();
			colName.add("NO");
			for(int i=1;i<=colCount;i++){
				colName.add(rsmd.getColumnName(i));
			}
			model = new DefaultTableModel(colName, 0) {
				// 셀 내용 수정 불가
				public boolean isCellEditable(int rowIndex, int mColIndex) {
					return false;
				}
			};
			int cnt=1;
			while(rs.next()){
				Vector vec = new Vector<>();
				vec.add(cnt);
				for(int i=1;i<=colCount;i++){
					vec.add(rs.getString(i));
				}
				model.addRow(vec);
				cnt++;
			}
			table.setModel(model);
			kekka.setText(model.getRowCount()+"건이 조회되었습니다");
			rs.close();
			psmt.close();
		} catch (Exception e) {
			kekka.setText(e.getMessage());
			e.printStackTrace();
		}
	}
}
